import wx
import wx.xrc
import os
import cv2 as cv
import numpy as np
import matplotlib as plt

class MyFrame(wx.Frame):

    def __init__(self, parent, id = wx.ID_ANY,title= wx.EmptyString, pos=wx.DefaultPosition,
                          size=(800,600),style=wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL):
        wx.Frame.__init__(self, parent, id=id, title=title, pos=pos,
                          size=size, style=style)
        self.panel = wx.Panel(parent=self)  # 面板

        self.fgSizer1 = wx.BoxSizer(wx.VERTICAL)
        # self.fgSizer1.SetFlexibleDirection(wx.BOTH)
        # self.fgSizer1.SetNonFlexibleGrowMode(wx.FLEX_GROWMODE_SPECIFIED)

        bSizer2 = wx.BoxSizer(wx.HORIZONTAL)

        self.fileName=""

        self.btn1 = wx.Button(self, wx.ID_ANY, u"选择、读取并显示图片", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer2.Add(self.btn1, 0, wx.ALL, 5)

        self.bnt2 = wx.Button(self, wx.ID_ANY, u"将真彩图站换成灰度图", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer2.Add(self.bnt2, 0, wx.ALL, 5)

        self.btn3 = wx.Button(self, wx.ID_ANY, u"将灰度图转换成二值图", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer2.Add(self.btn3, 0, wx.ALL, 5)

        self.btn4 = wx.Button(self, wx.ID_ANY, u"获取图像直方图", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer2.Add(self.btn4, 0, wx.ALL, 5)

        self. fgSizer1.Add(bSizer2, 1, wx.EXPAND, 5)
        self.fgSizer1.AddSpacer((0,0), 1, wx.EXPAND, 5)
        # self.bs = wx.BoxSizer(wx.HORIZONTAL)
        #
        # self.fgSizer1.Add(self.bs, 2, wx.EXPAND, 5)

        self.SetSizer(self.fgSizer1)
        self.Layout()

        self.Centre(wx.BOTH)

        # Connect Events
        self.btn1.Bind(wx.EVT_BUTTON, self.getChosenFileName)

    def __del__(self):
        pass

    def getChosenFileName(self, event):
        fd=wx.FileDialog(None,"请选择图像文件",os.getcwd(),wildcard="All files (*.*)|*.*", style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
        if fd.ShowModal() == wx.ID_OK:
            self.fileName=fd.GetPath()
            print(self.fileName)
        img=wx.Image(self.fileName,wx.BITMAP_TYPE_BMP)
        i=wx.StaticBitmap(self.panel, -1, wx.BitmapFromImage(img),pos=(500,400))
        bs=wx.BoxSizer(wx.HORIZONTAL)
        bs.Add(i,1,wx.EXPAND,5)
        self.fgSizer1.Add(bs,1,wx.EXPAND,5)
        self.panel.SetSizer(self.bs)
        self.panel.Layout()
        return self.fileName





if __name__ == '__main__':
    app=wx.App()
    mainWindow=MyFrame(None,title="第一个demo呀",size=(800,600))
    panel=wx.Panel(mainWindow)
    label = wx.StaticText(panel, label="Hello World", pos=(100, 100))
    img=wx.Image("./cat.bmp",wx.BITMAP_TYPE_ANY)
    w=img.GetWidth();
    h=img.GetHeight();
    mainWindow.Show(True)
    app.MainLoop()
